package main

import (
	"fmt"
	"strconv"
	"yundian/internel/app/dao"
	"yundian/internel/app/models/devrule"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/app/models/prices"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/utils"
)


type PlaceIdNameList struct {
	Id          int    `gorm:"column:id" json:"id"`
	PlaceName   string `gorm:"column:place_name" json:"placeName"`
}

type PlaceIdList struct {
	PlaceId     int    `gorm:"column:place_id" json:"placeId"`
}

func main() {
	dingo.InitViper("./configs/adm.yaml")
	dbs.NewMysqlComponent().Step()

	var lists []PlaceIdNameList

	sql := "SELECT id,place_name FROM places WHERE id IN (SELECT place_id FROM devices WHERE domain_url = 'https://dev.kdcgx.com/pcl/10032/dev/' and place_id > 0 GROUP BY place_id) and is_business != 3"

	global.Orm.Raw(sql).Find(&lists)

	rlst := GetPlaceIdsByUserId(221)
	rlst1 := GetPlaceIdsByUserId(219)

	var str string
	for _, lst := range lists {
		// 排除广西、海南
		if utils.ContainsInt(rlst, lst.Id) {
			continue
		}
		if utils.ContainsInt(rlst1, lst.Id) {
			continue
		}
		str = utils.Join(str, ",", strconv.Itoa(lst.Id))
	}

	fmt.Println(str)

}

// 去除南宁门店ID
func RemoveGxNnPlaceId(allPlaceId, nnPlaceId []int) []int {
	var ids []int
	tempMap := make(map[int]bool)
	for _, l := range allPlaceId {
		if _, ok := tempMap[l]; !ok {
			tempMap[l] = true
		}
	}

	for _, lst := range nnPlaceId {
		if _, ok := tempMap[lst]; ok {
			delete(tempMap, lst)
		}
	}

	for key := range tempMap {
		ids = append(ids, key)
	}

	return ids
}

func GetPlaceIdsByUserId(loginUserId int) []int {
	placesBranchIds := dao.NewUserDao().GetPlacesId(loginUserId).Unwrap().([]int) // 查询用户ID下的所有关联门店
	userIdArr := dao.NewUserDao().QueryChildUserIds(loginUserId).Unwrap().([]int) // 查询用户ID下所有下级用户ID
	placeIdArr := dao.NewPlaceDao().GetPlaceByIds(userIdArr).Unwrap().([]int)     // 下级用户ID 下的门店
	placesBranchIds = append(placesBranchIds, placeIdArr...)                  // 合并门店
	placesIds := utils.RemoveRepByMap(placesBranchIds)                        // 去重门店

	var ids []int
	db := global.Orm.Debug().Table("places").Select("id")
	db.Where("id in (?)", placesIds)

	db.Find(&ids)
	return ids
}

// 获取所有门店
func GetAllPlace() []placemodel.PlaceModel {
	var lists []placemodel.PlaceModel
	sql := `SELECT * FROM places`
	global.Orm.Raw(sql).Find(&lists)
	return lists
}

// 获取携程门店
func GetCtripPlace() []placemodel.PlaceModel {
	var lists []placemodel.PlaceModel
	sql := `SELECT * FROM places WHERE project_label = 'hotel_xc'`
	global.Orm.Raw(sql).Find(&lists)
	return lists
}

// 获取 15 to 20的门店
func GetGxPlace() []PlaceIdList {
	var lists []PlaceIdList
	sql := "SELECT place_id FROM prices WHERE `name` = '2022年国庆(15)to(20)' GROUP BY place_id"
	global.Orm.Raw(sql).Find(&lists)
	return lists
}


//func feemode()  {
//	lists := GetPlaceListByFeeMode(1)
//	//lists2 := GetPlaceListByFeeMode(2)
//	var data,data8,data10,data12 []int
//	for _,lst := range lists {
//		priceInfo := GetPriceInfoByPlaceId(lst.PlaceId)
//		if priceInfo != nil {
//			if priceInfo.Deposit < 1000 { // 小于10押金
//				if priceInfo.Deposit == 800 {
//					priceInfo.Id = 0
//					priceInfo.Deposit = 1500
//					priceInfo.MaxPayPrice = 1500
//					priceInfo.CreateAt = dingo.Time(time.Now().Unix())
//					priceInfo.Name = "2022年春节"
//					priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
//					if b := InsertIntoPlacePrice(priceInfo); b {
//						data8 = append(data8, priceInfo.PlaceId)
//					}
//				} else {
//					data = append(data, priceInfo.PlaceId)
//				}
//			} else {
//				if priceInfo.Deposit == 1000 {
//					priceInfo.Id = 0
//					priceInfo.Deposit = 1500
//					priceInfo.MaxPayPrice = 1500
//					priceInfo.CreateAt = dingo.Time(time.Now().Unix())
//					priceInfo.Name = "2022年春节"
//					priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
//					if b := InsertIntoPlacePrice(priceInfo); b {
//						data10 = append(data10, priceInfo.PlaceId)
//					}
//				}
//				if priceInfo.Deposit == 1200 {
//					priceInfo.Id = 0
//					priceInfo.Deposit = 1500
//					priceInfo.MaxPayPrice = 1500
//					priceInfo.CreateAt = dingo.Time(time.Now().Unix())
//					priceInfo.Name = "2022年春节"
//					priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
//					if b := InsertIntoPlacePrice(priceInfo); b {
//						data12 = append(data12, priceInfo.PlaceId)
//					}
//				}
//			}
//		}
//	}
//	log.Println("data", data)
//	log.Println("data8-押金为8元的门店", data8)
//	log.Println("data10-押金为10元的门店", data10)
//	log.Println("data12-押金为12元的门店", data12)
//}

// 获取所有门店 feeMode 1 计时  2套餐
func GetPlaceListByFeeMode(feeMode int) []int {
	var ids []int
	var lists []PlaceIdList
	sql := `select place_id from devices where place_id > 0 and fee_mode = ? group by place_id`
	global.Orm.Raw(sql, feeMode).Find(&lists)
	for _, lst := range lists {
		ids = append(ids, lst.PlaceId)
	}
	return ids
}


func GetPlaceListByFeeModeNotXc() []PlaceIdList {
	var lists []PlaceIdList
	sql := `SELECT place_id FROM devices WHERE place_id IN (SELECT id FROM places WHERE project_label != 'hotel_xc') and fee_mode = 2 GROUP BY place_id`
	global.Orm.Raw(sql).Find(&lists)
	return lists
}

// 获取价格 - 计时
func GetPriceInfoByPlaceId(placeId int) *prices.PriceModel {
	priceInfo := &prices.PriceModel{}
	sql := `select * from prices where place_id = ? and device_type_id = 10 and create_at > 0 and place_id > 0 order by create_at desc limit 1`
	global.Orm.Raw(sql, placeId).Find(priceInfo)
	return priceInfo
}

func InsertIntoPlacePrice(m *prices.PriceModel) bool {
	db := global.Orm.Debug().Table("prices").Create(m)
	if db.Error != nil {
		return false
	}
	return true
}

func GetRulesModel() []devrule.DeviceRulesModel {
	var lst []devrule.DeviceRulesModel
	sql := `select * from device_rule_gq WHERE type_id = 12`
	global.Orm.Raw(sql).Find(&lst)
	return lst
}
